// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../../public/public'),
		clientApi = require('../../public/clientapi');

	var ClientQuote = require('../../public/clientquote');

	function WorldModule () {
		this.codeName = {
			'HSI' : '恒生指数',
			'N225': '日经指数',
			'DJI': '道琼斯',
			'IXIC': '纳斯达克',
			'SPX': '标普500',
			'XIN9': '富时A50',
			'FTSE': '英国富时',
			'GDAXI': '德国DAX',
			'FCHI': '法国CAC'
		};
		this.callback = false;

		this.reqQuote = null;
		this.init.apply(this, arguments);
	}

	WorldModule.prototype.init = function (params) {
		this.callback = params.callback || false;
		var htmlDom = '<li data-mname="world">' +
					'<div class="module-item">' +
						'<div class="module-header">' +
							'<a href="###" class="icon module-close-btn"></a>' +
							'<p class="module-name">外盘指数</p>' +
						'</div>' +
						'<div class="module-main">' +
							'<div class="table-box">' +
								'<table class="table-head">' +
									'<thead>' +
										'<tr>' +
											'<td>市场</td>' +
											'<td>最新</td>' +
											'<td>涨跌</td>' +
											'<td>涨跌幅</td>' +
										'</tr>' +
									'</thead>' +
								'</table>' +
								'<div class="table-scroll">' +
									'<table>' +
										'<tbody id="worldIndexData">' +
											// '<tr>' +
											// 	'<td class="txt-l pdl10">恒生市场</td>' +
											// 	'<td class="txt-r pdr30">2369.20</td>' +
											// 	'<td class="txt-r pdr30">23.26</td>' +
											// 	'<td class="txt-r pdr30">0.69%</td>' +
											// '</tr>' +
										'</tbody>' +
									'</table>' +
								'</div>' +
							'</div>' +
						'</div>' +
					'</div>' +
				'</li>';
			if (!this.callback) {
				$('#modules-list-ul').append(htmlDom);
			}
			this.requestData();

	}
	WorldModule.prototype.requestData = function () {
		var self = this;

		var codeArr = ['HSI','N225','DJI','IXIC','SPX','XIN9','FTSE','GDAXI','FCHI'];
		self.requestQuote(codeArr);
	}

	WorldModule.prototype.renderData = function (data, orderType) {
		var self = this;
		var tbodyHtml = [];
		$.each(data, function (i, val) {
			var newprice = val['newprice'] == '--' ? '--' : val['newprice'].toFixed(2);
			var zdf = val['zdf'] == '--' ? '--' : val['zdf'].toFixed(2) + '%';
			var zd = val['zd'] == '--' ? '--' : val['zd'].toFixed(2);
			var code = val['code'];

			var zdfColor = '';
			if (val['zdf'] > 0) {
				zdfColor = 'red';
			} else if (val['zdf'] < 0) {
				zdfColor = 'green';
			}
			var trHtml = '<tr data-code="' + code + '">' +
							'<td class="txt-c pdl10">' + self.codeName[code] + '</td>' +
							'<td class="txt-r pdl30">' + newprice + '</td>' +
							'<td class="txt-r pdr30 '+ zdfColor +'">' + zd + '</td>' +
							'<td class="txt-r pdr30 '+ zdfColor +'">' + zdf + '</td>' +
						'</tr>';
			tbodyHtml.push(trHtml);
		});
		$('#worldIndexData').html(tbodyHtml.join(''));
	}

	WorldModule.prototype.requestQuote = function (codeArr) {
		var self = this;
		var code = codeArr.join(',');
		var quote = 'ZQMC,NEW,ZHANGDIEFU,SETTLE,PRE';
		var getQuote = 'ZQMC,NEW,ZHANGDIEFU,QHZD';
		var reqCallback = function(data) {
			// alert(JSON.stringify(data));
			var tableData = [];
			$.each(codeArr, function(i, val) {
				if (!data[val]) {
					return true;
				}
				var tempObj = {};
				var zqmc = self.codeName[val];

				var newprice = parseFloat(data[val]['NEW']);
				if (isNaN(newprice)) {
					newprice = '--';
				}

				var zdf = parseFloat(data[val]['ZHANGDIEFU']);
				if (isNaN(zdf)) {
					zdf = '--';
				}

				var zd = parseFloat(data[val]['QHZD']);
				if (isNaN(zd)) {
					zd = '--';
				}

				tempObj['code'] = val;
				tempObj['newprice'] = newprice;
				tempObj['zdf'] = zdf;
				tempObj['zd'] = zd;
				tableData.push(tempObj);
			});
			if (self.callback) {
				self.callback(tableData);
			} else {
				self.renderData(tableData);
			}
		};
		var isNeedPush = true;
		if (self.callback) {
			isNeedPush = false;
		}
		var params = {
			stocks: code,
			quote: quote,
			getQuote: getQuote,
			period: 'now',
			callback: reqCallback,
			isNeedPush: isNeedPush
		};

		if (self.reqQuote) {
			self.reqQuote.unregisterPush();
		}
		try {
			self.reqQuote = new ClientQuote(params);
			self.reqQuote.request();
		} catch (e) {}
	}

	WorldModule.prototype.backPage = function () {
		this.requestData();
	}
	WorldModule.prototype.leavePage = function () {
		this.reqQuote.unregisterPush();
	}

	WorldModule.prototype.updateSeries = function (isHasOverall, date) {

	}

	module.exports = WorldModule;
});